package online.shenjian.mq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "spring.rabbitmq.enable", matchIfMissing = true)
public class RabbitSender {

    private RabbitTemplate rabbitTemplate;

    public RabbitSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * 不显式声明交换机时并且发送消息不指定交换机，则默认使用Direct，
     * 并且声明队列时，不显式绑定队列与交换机，则队列以队列名为routing-key
     * 绑定到默认的direct交换机，
     * 发送消息不指定交换机时，则将消息发到默认的direct交换机
     */
    public void send() {
        String msg = "Hello World!";
        this.rabbitTemplate.convertAndSend("hello", msg);
        System.out.println("发送消息" + msg + "至队列hello");
    }
}
